package asionreachel.cn.smarthomedemo.monitor;

import android.os.Handler;

import asionreachel.cn.smarthomedemo.base.Common;
import familyclient.android.loginmanager.JLoginManagerInterface;
import familyclient.android.loginmanager.JLoginManagerListener;
import familyclient.android.netswitcher.JNetSwitcherInterface;
import familyclient.android.netswitcher.JNetSwitcherListener;

public class JLoginMonitor {
    private static JLoginMonitor s_Instance = null;
    private final static int s_iCheckLoginedTime = 30 * 1000;

    public static JLoginMonitor GetInstance() {
        if(s_Instance==null){
            s_Instance=new JLoginMonitor();
        }
        return s_Instance;
    }

    private boolean m_bAutoLogin = true;
    private boolean m_bLastCheckLogined = false;

    private JNetSwitcherListener m_listenerNetSwitcher = new JNetSwitcherListener() {
        @Override
        public void CbInternetServerDisconnectCb() {
            m_bLastCheckLogined = false;
        }
    };

    private JLoginManagerListener m_listenerLoginManager = new JLoginManagerListener() {
        @Override
        public void CbBeenLogoutCb( int a_iReason ) {
            m_bLastCheckLogined = false;
        }

        @Override
        public void FamilyLoginCb( String a_strFamilyID, String a_strErr ) {
            if( null == a_strFamilyID || (null != a_strErr && a_strErr.length() > 0) ) {
                m_bLastCheckLogined = false;
                return;
            }

            m_bLastCheckLogined = true;
            Common.s_bDirectConnect = false;
            Common.s_strFamilyID = a_strFamilyID;
        }

        @Override
        public void LogoutCb(String a_strErr) {
            if( null != a_strErr && a_strErr.length() > 0 )
                return;

            m_bLastCheckLogined = false;
        }

        @Override
        public void CheckFamilyLoginedCb( boolean a_bLogined ) {
            m_bLastCheckLogined = a_bLogined;

            if( !m_bAutoLogin || a_bLogined )
                return;

            String strAccount = Common.getAccountId();
            String strPassword = Common.getPassword();
            if( null == strAccount || 0 == strAccount.length() || null == strPassword || 0 == strPassword.length() )
                return;

            String strFamilyID = "";
            String strPhone = "";
            String strEmail = "";
            if (Common.checkEmail(strAccount))
                strEmail = strAccount;
            else if (Common.checkPhoneNumber(strAccount))
                strPhone = strAccount;
            else
                strFamilyID = strAccount;

            JLoginManagerInterface.GetInstance().FamilyLogin( strFamilyID, strPhone, strEmail, strPassword );
        }
    };

    private Handler m_handlerCheckLogined = new Handler();
    private Runnable m_runnableCheckLogined = new Runnable() {
        @Override
        public void run() {
            m_handlerCheckLogined.postDelayed( m_runnableCheckLogined, s_iCheckLoginedTime );
            JLoginManagerInterface.GetInstance().CheckFamilyLogined();
        }
    };

    public JLoginMonitor() {
    }

    public void Init() {
        s_Instance = this;
        m_bAutoLogin = true;
        JNetSwitcherInterface.GetInstance().AddListener( m_listenerNetSwitcher );
        JLoginManagerInterface.GetInstance().AddListener( m_listenerLoginManager );
        m_handlerCheckLogined.postDelayed( m_runnableCheckLogined, 0 );
    }

    public void UnInit() {
        JLoginManagerInterface.GetInstance().DelListener( m_listenerLoginManager );
        JNetSwitcherInterface.GetInstance().DelListener( m_listenerNetSwitcher );
        s_Instance = null;
    }

    public void SetAutoLogin( boolean a_bAutoLogin ) {
        m_bAutoLogin = a_bAutoLogin;
    }

    public boolean GetAutoLogin() {
        return m_bAutoLogin;
    }

    public boolean GetLastCheckLogined() {
        return m_bLastCheckLogined;
    }
}
